Introduction to Machine Learning

Unit 22: DBSCAN

1. Introduction

This unit introduces a fundamentally different clustering algorithm: DBSCAN. Unlike the methods discussed earlier, DBSCAN identifies clusters from local density and can explicitly flag noise or outlier points. We will see how it discovers clusters of arbitrary shape and how to select its hyperparameters MinPts and \( \varepsilon \) (epsilon) using the K-distance graph.

Learning Objectives

Today's Agenda

  1. DBSCAN algorithm & its point taxonomy
  2. Definitions: directly density-reachable, density-reachable, density-connected
  3. Identifying optimal ε and MinPts: rule-of-thumb & K-distance graph / elbow
  4. K-distance graph example on Iris dataset
  5. DBSCAN vs K-Means comparison

2. Theory

2.1 Silhouette Recap (from Unit 24)

For each cluster \(C_j\), the average silhouette width is the mean of the individual silhouette values for the points in that cluster: \[ \bar{s}(C_j) = \frac{1}{|C_j|} \sum_{d_i \in C_j} s(i) \] The global average silhouette width is then \[ \text{ASW} = \frac{1}{n} \sum_{i=1}^{n} s(i) \] These averages summarize cluster quality at both the individual-cluster and overall levels. Computationally, silhouette is expensive for large datasets (O(n²) distance evaluations).

2.5 DBSCAN — Density-Based Spatial Clustering of Applications with Noise

DBSCAN assigns each data point to one of three point types based on two hyperparameters: the radius ε (epsilon) and the minimum number of points MinPts required for a dense neighborhood. The three types are core, border, and noise points.

1. Core Points
2. Border Points
3. Noise Points

A point q is a core point if its ε-neighborhood contains at least MinPts points (counting q itself).

Property: Core points lie in the interior of a dense region and serve as the "seeds" from which clusters are grown.

A point p is a border point if its own ε-neighborhood contains < MinPts points, but there is a chain of direct density-reachability links from a core point to p.

Property: Border points lie at the edge of a dense region. They belong to a cluster, but they cannot extend that cluster further.

A point is a noise point (outlier) if it is neither a core point nor reachable from any core point.

Property: In sklearn, noise points are explicitly assigned a cluster label of −1. Thus, DBSCAN can leave such points outside all discovered clusters.

DBSCAN: Three Point Types A visual explanation of DBSCAN showing core points, a border point, noise, and epsilon neighborhoods for two core points. DBSCAN: three point types MinPts = 4 · ε neighborhoods shown as dashed circles ε C1 Border point Inside ε of core C1, but its neighborhood contains fewer than 4 points. Noise Not core or reachable ε C2 Outlier · label −1 Not a core point and not reachable Core point Border point Noise Outlier

2.6 DBSCAN Key Definitions

  1. Directly density-reachable: p is directly density-reachable from q if:
    • q is a core point, AND
    • p is within ε of q (p ∈ N_ε(q)).
    Note: "directly reachable" is NOT symmetric. A core can reach a border, but a border cannot reach back because it is not a core point.
  2. Density-reachable: p is density-reachable from q if there exists a chain of points q → q₁ → q₂ → … → q_k → p such that each adjacent step is directly density-reachable. This relation is still asymmetric in general.
  3. Density-connected: p and q are density-connected if there exists some core point o such that BOTH p and q are density-reachable from o. This relation is symmetric and captures the "same cluster" relationship.
  4. A DBSCAN cluster: A maximal set of density-connected points.
Density-reachability chain A density-reachability chain from core A through intermediate points and core C to border point D, showing directional reachability and shared cluster membership. DENSITY-REACHABILITY CHAIN ε radius drawn around each point DIRECT DENSITY-REACHABILITY directly directly directly directly core A core point core C core point border D not a core point ε-neighborhood ε-neighborhood ↗ Self-reachability A is density-reachable from A — trivially. A → A → Forward reachability D is density-reachable from A via A → … → C → D. A → … → D × No back-link A is not density-reachable from D: D is not core. D ↛ A SAME CLUSTER A and D are density-connected via a shared core o = A. A ≡ D

2.7 DBSCAN Example 1: Manual Execution

Consider the 5 2D points A(1,4), B(2,3), C(1,5), D(5,5), and E(8,1), with
MinPts = 2 and ε = 2 (using Euclidean distance).

Step 0: Identify the core points. For each point, count the points in its ε-neighborhood (distance ≤ 2), including the point itself:

Step 1: Grow the cluster from the core points. Starting from a core point, include points that are directly density-reachable within ε and continue the expansion through core points.

  1. Start with core A and create Cluster 1. Add A, then expand to B and C, which are directly reachable. Because B and C are already core points and have no new points within ε, the expansion is complete.
  2. D has no other neighbors besides itself, so mark it as noise.
  3. E has no other neighbors besides itself, so mark it as noise.
PointCoordinatesTypeFinal Label
A(1, 4)CoreCluster 1
B(2, 3)CoreCluster 1
C(1, 5)CoreCluster 1
D(5, 5)Noise−1 (noise)
E(8, 1)Noise−1 (noise)

2.8 Selecting DBSCAN Hyperparameters

Selecting MinPts

Selecting ε via the K-Distance Graph

For each point in the dataset, compute the distance to its k-th nearest neighbor, with k = MinPts (or k = MinPts − 1 depending on convention). Then sort all the resulting k-distances in ascending order and plot them. Use the elbow of this curve to choose ε.

K-distance graph for k equals 3 Sorted third-nearest-neighbor distances plotted against point index, showing an elbow near epsilon equals 0.8 and increasing distances for outliers. K-distance graph (k = 3) Sorted 3rd-nearest-neighbor distances versus point index ε ≈ 0.8 0.0 0.5 1.0 1.5 2.0 2.5 3.0 distance sorted point index → ELBOW ≈ 0.8 pick ε ≈ 0.8 points in dense clusters transition outliers (noise) large k-distance

Iris K-distance Graph Case Study

2.9 DBSCAN vs K-Means: Side-by-Side

AspectK-MeansDBSCAN
Requires K specified beforehand?YesNo (discovers K automatically from density structure)
Assumes spherical / convex clusters?Yes (centroid + SSE)No (finds arbitrarily shaped clusters — even nested / crescent shapes)
Sensitive to outliers?Very (outliers pull centroids toward them)Robust (explicitly marks outliers as noise / −1)
Forces every point into a cluster?Yes (hard assignment)No (points can remain as noise)
Struggles with arbitrary / non-convex shapes?Yes (splits them unnaturally)Excellent at non-convex and nested shapes
Memory usageLowNeeds distance matrix or spatial index (can be high)
Speed / ScalabilityVery fast (linear in n × iter)Slower (range queries needed)
Interpretable cluster centers?Yes (centroids are meaningful)No real "center" (harder to explain to business stakeholders)

3. Interactive Examples

Example 1: Purity of "one cluster per point"

A student claims "I can always achieve perfect purity, regardless of the dataset." Is this possible? If yes, construct it. If not, explain.

Yes, trivially: set K = n (each point its own singleton cluster).

In each singleton cluster, the single point has exactly one true label, so max_j |C_i ∩ L_j| = 1 for every cluster. Sum of maxima = n, so Purity = n/n = 1. The result shows why purity alone is misleading: splitting the data into more clusters can make the score look perfect without reflecting useful clustering structure. Therefore, use it together with Adjusted Rand Index, Silhouette, or metrics that penalize the use of more clusters.

Example 2: DBSCAN MinPts Intuition

A 7-dimensional dataset is to be clustered with DBSCAN. Which MinPts value is the most reasonable starting point: 1, 2, 4, or 100?

Reveal Answer

MinPts = 4. The rule of thumb: MinPts ≥ d+1 = 8, but 4 is close and a standard starting value (MinPts ≥ 4 or 5 for high dim). The choices illustrate the trade-off: very small values make neighborhoods too permissive, while a very large value can make dense regions fail the MinPts requirement. Why not the others?

  • MinPts = 1: every point is its own "core" → degenerate; every point forms its own cluster / no structure.
  • MinPts = 2: borderline; very sensitive to noise.
  • MinPts = 100: too large — many truly dense regions will have fewer than 100 neighbors within any reasonable ε → everything becomes noise.

This makes MinPts an important density threshold: the chosen value affects whether local neighborhoods are treated as sufficiently dense to form clusters.

Example 3: K-Means vs DBSCAN on two moons

The classic "two interleaved half-moons" dataset has two non-convex crescent-shaped clusters. Which algorithm will recover the two moons correctly, and why?

DBSCAN will recover the two moons perfectly (with appropriate MinPts and ε):

  • Each crescent is a uniformly dense region → within each moon, every interior point is a core; the entire crescent is density-connected.
  • Between the two crescents there's a gap → no density bridge → DBSCAN correctly separates them into two clusters.

The key point is that DBSCAN follows the density-connected structure of the data rather than forcing each cluster to be represented by a centroid.

K-Means with K=2 will fail: it splits each crescent through the middle and produces two "half-moon sliced" clusters, because the centroids migrate to the overall arithmetic means of each half of the plane, which don't respect the shape.

Example 4: Rand Index edge case — perfect clustering

True labels: 4 points form 2 natural classes. Clustering produced also 2 clusters identical to the true classes. What is the Rand Index? (Compute explicitly.)

Reveal Answer

Points: p1,p2 in L1; p3,p4 in L2. Same for clusters C1={p1,p2}, C2={p3,p4}.

6 pairs:

PairClustered together?Label together?Type
(1,2)YesYesTP
(1,3)NoNoTN
(1,4)NoNoTN
(2,3)NoNoTN
(2,4)NoNoTN
(3,4)YesYesTP

TP=2, TN=4, FP=0, FN=0.

\[ \text{Rand Index} = \frac{2+4}{2+4+0+0} = 1.00 \]

As expected, every pair has the same relationship in the produced clustering and the true labels, so the Rand Index reaches 1.

4. Numerical Solutions

Problem 1: Purity from 3×2 contingency table

Contingency table (rows = produced clusters, cols = true labels):

ClusterLabel XLabel YTotal
C18210
C23710
C35510
Label total1614n = 30

Compute the Purity from the contingency table.

📘 Step-by-Step Solution

Step 1: Find the largest class count in each cluster.

  • C1: max(8,2) = 8
  • C2: max(3,7) = 7
  • C3: max(5,5) = 5 (ties broken arbitrarily since value is same)

Step 2: Sum the maxima to obtain 8 + 7 + 5 = 20.

Step 3: Divide by n:

\[ \text{Purity} = \frac{20}{30} \approx 0.667 \]

Problem 2: DBSCAN class identification

Consider six 1D points on a number line at positions {1, 2, 3, 6, 10, 11}. Use MinPts=3 and ε=1.2 (distance = absolute difference). Classify each point as Core, Border, or Noise, and then list the clusters found.

📘 Step-by-Step Solution

Step 1: For each point, count points within ε=1.2 (including itself).

PointPosNeighbors (|x − pos| ≤ 1.2)CountCore?
p11{1,2}2 < 3No
p22{1,2,3}3 ≥ 3✅ YES CORE
p33{2,3}2 < 3No
p46{6}1 < 3No
p510{10,11}2 < 3No
p611{10,11}2 < 3No

Step 2: Distinguish border points from noise. p2 is the only core point.

  • p1 is within ε of core p2 (|1−2|=1 ≤ 1.2) → Border of the same cluster.
  • p3 is within ε of core p2 (|3−2|=1 ≤ 1.2) → Border.
  • p4 is not a core, and its distance to the nearest core (p2) is 4 > 1.2, so no core can reach it → Noise.
  • p5 is at distance 8 from p2, so it is not reachable from the only core → Noise.
  • p6 is at distance 9 from p2, so it is not reachable from the only core → Noise.

Clusters found: One cluster, Cluster 1 = {p1, p2, p3}; the remaining points are noise: {p4, p5, p6} (label -1). This final grouping follows directly from the single core point and the two border points reachable from it.

Problem 3: Rand Index & Jaccard on 4 points

True labels: L1 = {a, b}, L2 = {c, d}.
Produced clustering: C1 = {a, c}, C2 = {b}, C3 = {d} (K=3 produced).

Compute TP, TN, FP, and FN, and then calculate the Rand Index and Jaccard coefficient.

📘 Step-by-Step Solution

6 total pairs:

PairSame cluster?Same label?Type
(a,b)No (C1 vs C2)Yes (L1)FN
(a,c)Yes (C1)No (L1 vs L2)FP
(a,d)No (C1 vs C3)No (L1 vs L2)TN
(b,c)No (C2 vs C1)No (L1 vs L2)TN
(b,d)No (C2 vs C3)No (L1 vs L2)TN
(c,d)No (C1 vs C3)Yes (L2)FN

Counts: TP=0, TN=3, FP=1, FN=2. Total = 6.

\[ \text{Rand Index} = \frac{0 + 3}{0 + 3 + 1 + 2} = \frac{3}{6} = 0.5 \] \[ \text{Jaccard} = \frac{0}{0 + 1 + 2} = 0 \]

Interpretation: Rand 0.5 is essentially random-level agreement on this tiny dataset. Jaccard is 0 because the produced clustering put no pair together that should have been together. The two metrics therefore agree that the clustering shows little useful pairwise agreement with the true labels.

5. Try It Yourself

Practice 1: Purity calculation 2×3

Contingency table (clusters × labels):

RedGreenBlueTotal
Cluster A91111
Cluster B18514
Total109625

Compute the Purity and round the result to 3 decimals.

For Cluster A, the largest class count is max(9,1,1) = 9.

For Cluster B, the largest class count is max(1,8,5) = 8.

The sum is 17, with n = 25.

\[ \text{Purity} = \frac{17}{25} = 0.680 \]
Practice 2: DBSCAN MinPts=4

Consider the 2D points A(0,0), B(1,0), C(0,1), D(1,1), and E(5,5), with ε=1.5 and MinPts=4. Classify each point and describe the resulting clusters.

Consider the ε-radius around each point:

  • A: N includes A,B,C,D (distances: 0, 1, 1, √2 ≈ 1.41 ≤ 1.5), giving 4 points ≥ 4 → Core.
  • B: its neighbors are A, B, D, C (same distances), giving 4 → Core.
  • C: its neighbors are A, C, D, B (same distances), giving 4 → Core.
  • D: its neighbors are A, B, C, D, giving 4 → Core.
  • E(5,5): the distance to the nearest others is √((5−1)²+(5−1)²) ≈ 5.66 > 1.5, so only itself is in its ε-neighborhood. It is NOT core and is not reachable from any core. → Noise.

Result: One cluster = {A,B,C,D}; E is noise (−1). The four nearby points reinforce one another's core status, while E has no connection to that dense region.

Practice 3: Adjusted Rand intuition via Rand baseline

We'll skip the exact ARI formula in this course and explain it qualitatively: If RI = 0.86 on a dataset, why might the Adjusted Rand Index (ARI) be only 0.58, and why do we prefer the adjusted version?

The plain Rand Index is dominated by TN (pairs that both methods put in different groups). In typical datasets with many classes, MOST pairs are in different true classes, and MOST pairs are also in different clusters — so even random clusterings can have a high Rand index merely by "mostly saying no." This makes RI less informative when the large number of TN pairs dominates the score.

The Adjusted Rand Index (ARI) corrects this by subtracting the expected RI under a random-partition baseline and normalizing, so that ARI ≈ 0 for random independent partitions and ARI = 1 only for perfect agreement. This makes the adjusted score more useful when we want agreement beyond what can be attributed to the random-partition baseline. This is why ARI (and not plain RI) is the standard in scikit-learn's adjusted_rand_score.

6. Interactive Quiz

Your score: 0 / 5

7. Key Takeaways

  1. DBSCAN has 3 point types: Core (≥ MinPts in an ε-ball), Border (in a core's ε-ball but not a core), and Noise (−1, everything else). This point taxonomy determines whether a point helps grow a cluster, belongs to its boundary, or remains outside all clusters.
  2. DBSCAN key relationships: directly density-reachable (core→within ε), density-reachable (chain of direct), and density-connected (mutually reachable from some core, symmetric → defines a cluster). These relationships explain how local density links individual points into a cluster.
  3. Set MinPts ≥ d+1 (typically 4 or 5). Set ε from the elbow of the sorted k-distance (k ≈ MinPts) curve where dense regions transition to sparse outliers. The elbow is useful because it marks the change from the small distances typical of dense regions to the larger distances of sparse points.
  4. DBSCAN auto-discovers K, handles arbitrary shapes, and marks outliers explicitly; K-Means requires K, assumes spherical clusters, and forces every point into a cluster. The comparison is therefore mainly about how each method defines and assigns clusters.

8. Common Pitfalls

  1. Forgetting to count the point ITSELF in MinPts: "ε-neighborhood size ≥ MinPts" includes the query point. A point with 2 other neighbors within ε counts MinPts=3, not 2. This detail directly affects whether the point is classified as core.
  2. Misunderstanding "directly density-reachable" as symmetric: It is not. A border point is within ε of a core (core→border is "direct"), but the reverse step is invalid because the border is not itself a core.
  3. Running DBSCAN on unscaled data: ε is a Euclidean radius, so feature scales matter. StandardScaler / MinMaxScaler first. Otherwise, the same ε can represent very different neighborhoods across features.
  4. Picking ε too small / too large: Too small → almost everything is noise (−1); too large → all dense points merge into one giant cluster. Use the K-distance elbow rather than guessing.
  5. Rand Index dominance by TNs: With many classes, TN dominates RI, making random-looking splits score high anyway. Prefer ARI when TN dominance makes the plain RI hard to interpret.